package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //1. 用户列表查询
    @GetMapping
    public Result getList(UserDto userDto){
        log.info("用户列表请求信息:{}",userDto);
        PageResult<User> pageResult =userService.getList(userDto);
        return Result.success(pageResult);
    }

    //2. 删除用户
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){//或Integer[]接收
        log.info("删除用户ids:{}",ids);
        userService.deleteById(ids);
        return Result.success();
    }



    //3. 添加用户
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("添加用户数据:{}",user);
        userService.addUser(user);
        return Result.success();
    }

    //4. 根据ID查询
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id){
        log.info("查询用户id:{}",id);
        User user=userService.getUserById(id);
        return Result.success(user);
    }

    //5. 修改课程
    @PutMapping
    public Result updateById(@RequestBody User user){
        log.info("修改用户:{}",user);
        userService.updateById(user);
        return Result.success();
    }

    //7. 根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result getListUserByRoleId(@PathVariable String roleLabel){
        log.info("根据角色查询用户:{}",roleLabel);
        List<User> list=userService.getListUserByRoleId(roleLabel);
        return Result.success(list);
    }
}
